package cn.com.tom.servlet;

import cn.com.tom.bean.Contact;
import cn.com.tom.bean.ContactParams;
import cn.com.tom.bean.ContactResult;
import cn.com.tom.bean.User;
import cn.com.tom.service.ContactService;
import cn.com.tom.service.UserService;
import cn.com.tom.service.WebsiteService;
import cn.com.tom.service.impl.ContactServiceImpl;
import cn.com.tom.service.impl.UserServiceImpl;
import cn.com.tom.service.impl.WebsiteServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/contact/*")
public class ContactServlet extends BaseServlet {

    ContactService servie = new ContactServiceImpl();
    UserService userService = new UserServiceImpl();
    WebsiteService websiteService = new WebsiteServiceImpl();


    public void admin(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ContactParams contactParams = new ContactParams();
        BeanUtils.populate(contactParams, request.getParameterMap());
        ContactResult result = ((ContactServiceImpl) servie).list(contactParams);
        request.setAttribute("result", result);
        forward(request, response, "admin/contact.jsp");
    }

    public void save(HttpServletRequest request, HttpServletResponse response) throws Exception {

        String captcha = (String) request.getSession().getAttribute("captcha");
        String code = request.getParameter("captcha");
        if (!code.toLowerCase().equals(captcha)) {
            forwardError(request, response, "验证码有误");
            return;
        }

        Contact bean = new Contact();
        BeanUtils.populate(bean, request.getParameterMap());
        if (servie.save(bean)) {
            redirect(request, response, "/contact/front");
        } else {
            forwardError(request, response, "保存失败");
        }
    }

    public void remove(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String[] idS = request.getParameterValues("id");
        List<Integer> list = new ArrayList();
        for (int i = 0; i < idS.length; i++) {
            list.add(Integer.valueOf(idS[i]));
        }
        if (servie.remove(list)) {
            redirect(request, response, "/contact/admin");
        } else {
            forwardError(request, response, "删除失败");
        }
    }

    public void front(HttpServletRequest request, HttpServletResponse response) throws Exception {
        User user = userService.list().get(0);
        request.setAttribute("user", user);
        request.setAttribute("website", websiteService.list().get(0));
        forward(request, response, "front/contact.jsp");
    }

    public void read(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Integer id = Integer.valueOf(request.getParameter("id"));
        Map<String, Object> params = new HashMap<>();
        if (servie.read(id)) {
            params.put("msg", "更新成功");
            params.put("success", true);
        } else {
            params.put("msg", "更新失败");
            params.put("success", false);
        }
        response.setContentType("text/json;charset=UTF-8");
        ObjectMapper objectMapper = new ObjectMapper();
        response.getWriter().write(objectMapper.writeValueAsString(params));
    }
}
